package com.aaa.stock.service.Impl;


import com.aaa.entity.Producer;
import com.aaa.stock.dao.ProducerDao;
import com.aaa.stock.service.ProducerService;
import com.aaa.stock.vo.Producervo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

@Service
public class ProducerServiceImpl extends ServiceImpl<ProducerDao,Producer> implements ProducerService  {
    @Autowired
    private ProducerDao producerDao;



    @Override
    public Result<IPage<Producer>> findByCondition(Integer currentPage, Integer pageSize, Producervo producervo) {
        IPage<Producer> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Producer> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(producervo.getProducerName())){
            wrapper.like("producer_name",producervo.getProducerName());
        }
        if (StringUtils.hasText(producervo.getKeywords())){
            wrapper.like("keywords",producervo.getKeywords());
        }
        if (StringUtils.hasText(producervo.getProducerTel())){
            wrapper.like("producer_tel",producervo.getProducerTel());
        }
        if (StringUtils.hasText(producervo.getStatus())){
            wrapper.eq("status",producervo.getStatus());
        }
        if (Objects.nonNull(producervo.getDateRange()) && producervo.getDateRange().length==2){
            wrapper.between("create_time",producervo.getDateRange()[0],producervo.getDateRange()[1]);
        }

        IPage<Producer> page1 = producerDao.selectPage(page, wrapper);
        return new Result<IPage<Producer>>(200,"查询用户成功",page1);
    }

    @Override
    public Result<List<Producer>> getAll() {
        List<Producer> producers = producerDao.selectList(null);
        return new Result<>(200,"查询",producers);
    }

}

